package com.yxhl.zoume.core.officialcar.presenter;

import android.content.Context;

import com.yxhl.protobuf.BizOrder;
import com.yxhl.protobuf.BizOrderRequest;
import com.yxhl.protobuf.BizOrderResponse;
import com.yxhl.protobuf.CommonRespParams;
import com.yxhl.protobuf.OrderBizType;
import com.yxhl.protobuf.OrderSrcType;
import com.yxhl.zoume.common.presenter.BasePresenter;
import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.core.officialcar.view.CreateOfficialCarOrderView;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.car.officialcar.CreateOfficialCarOrderUseCase;
import com.yxhl.zoume.utils.LOG;
import com.yxhl.zoume.utils.TimeUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;

/**
 * Created by sugarya on 16/8/25.
 */
public class CreateOfficialCarOrderPresenter extends BasePresenter {

    private static final String TAG = "CreateOfficialCarOrderPresenter";


    @Inject
    CreateOfficialCarOrderUseCase mCreateOfficialCarOrderUseCase;

    private CreateOfficialCarOrderView mCreateOfficialCarOrderView;

    /**
     * 他人手机号
     */
    private String otherMobile = "";
    /**
     * 是否需要为他人叫车
     */
    private boolean needCallCarForOthers = false;

    /**
     * 用户选择的车型
     */
    private String vehicleType;

    /**
     * 是否需要短信通知
     */
    private boolean needSendMsg;

    /**
     * 出发时间
     */
    private String departTime;

    /**
     * 出发地点
     */
    private String startLocation;

    /**
     * 到达地点
     */
    private String endLocation = "";

    /**
     * 出发地经度
     */
    private String lng;

    private String lat;

    /**
     * 到大地经度
     */
    private String endLng = "";

    private String endLat = "";

    /**
     * 租车时长
     */
    private String rentCarTime;


    @Inject
    public CreateOfficialCarOrderPresenter(Context context) {
        super(context);
    }


    public void createOfficialCarOrder(final OrderBizType orderBizType) {
        mCreateOfficialCarOrderView.showWaitingDialog(true);

        BizOrderRequest.Builder bizOrderRequestBuilder = BizOrderRequest
                .newBuilder()
                .setCommonReqParams(getCommonReqParams())
                .setBizType(orderBizType)
                .setVhlType(vehicleType)
                .setMobile(getMobile())
                .setOtherMobile(otherMobile)
                .setCallForOther(needCallCarForOthers)
                .setSendMsgFlag(needSendMsg)
                .setDepartTime(departTime)
                .setStart(startLocation)
                .setEnd(endLocation)
                .setLng(lng)
                .setLat(lat)
                .setElng(endLng)
                .setElat(endLat)
                .setSrc(OrderSrcType.APP);

        switch (orderBizType) {
            case OFFICIAL_BUS_TIME:
                bizOrderRequestBuilder.setRentCarTm(rentCarTime);
                break;
        }

        mCreateOfficialCarOrderUseCase.execute(bizOrderRequestBuilder.build(),new ZMSubscriber<BizOrderResponse>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mCreateOfficialCarOrderView.renderOfficialCarOrderOnError(e.toString());
                    }

                    @Override
                    public void onNext(BizOrderResponse bizOrderResponse) {
                        super.onNext(bizOrderResponse);
                        CommonRespParams commonRespParams = bizOrderResponse.getCommonRespParams();
                        String isSucc = commonRespParams.getIsSucc();
                        if ("true".equalsIgnoreCase(isSucc)) {
                            List<BizOrder> orderFieldsList = bizOrderResponse.getOrderFieldsesList();
                            if (orderFieldsList != null && orderFieldsList.size() > 0) {
                                mCreateOfficialCarOrderView.showWaitingDialog(false);
                                BizOrder bizOrder = orderFieldsList.get(0);
                                mCreateOfficialCarOrderView.renderOfficialCarOrderOnSuccess(bizOrder.getOrderSerial());
                            }
                        } else {
                            String resultMsg = commonRespParams.getResultMsg();
                            String resultCode = commonRespParams.getResultCode();
                            LOG.d(TAG, resultCode);
                            mCreateOfficialCarOrderView.renderOfficialCarOrderOnFailure(resultMsg);
                        }
                    }
                });

    }

    /**
     * 转换出行时间选择器 时间格式
     *
     * @param options1
     * @param options2
     * @param options3
     */
    public void handleZMTimePickerSelectResult(String options1, String options2, String options3) {
        StringBuilder sb = new StringBuilder();
        if ("现在".equals(options1)) {
            sb.append(TimeUtils.exchangeToTime(System.currentTimeMillis()));
        } else {
            sb.append(TimeUtils.getCurrentYear());
            sb.append("-");
            sb.append(options1.replace("月", "-").replace("日", " "));
            sb.append(options2.replace("点", ":"));
            sb.append(options3.replace("分", ""));
            sb.append(":00");
        }
        departTime = sb.toString();
    }

    /**
     * 转换分时租车选择器 时间格式
     *
     * @param options1
     */
    public void handleRentHourlyTimePickerSelectResult(String options1) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(options1);
        if (matcher.find()) {
            rentCarTime = matcher.group();
        }
    }


    @Override
    public void attachView(BaseView view) {
        if (view instanceof CreateOfficialCarOrderView) {
            mCreateOfficialCarOrderView = (CreateOfficialCarOrderView) view;
        }
    }


    public void setOtherMobile(String otherMobile) {
        this.otherMobile = otherMobile;
    }

    public void setNeedCallCarForOthers(boolean needCallCarForOthers) {
        this.needCallCarForOthers = needCallCarForOthers;
    }

    public void setVehicleType(String vehicleType) {
        this.vehicleType = vehicleType;
    }

    public void setNeedSendMsg(boolean needSendMsg) {
        this.needSendMsg = needSendMsg;
    }

    public void setStartLocation(String startLocation) {
        this.startLocation = startLocation;
    }

    public void setEndLocation(String endLocation) {
        this.endLocation = endLocation;
    }

    public void setLng(String lng) {
        this.lng = lng;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public void setEndLng(String endLng) {
        this.endLng = endLng;
    }

    public void setEndLat(String endLat) {
        this.endLat = endLat;
    }

}
